
//判断字符串是否为空
function isEmpty(strVal) {
    if (strVal == '' || strVal == null || strVal == undefined) {
        return true;
    } else {
        return false;
    }
}

function reSelect(id,uploaderid){
	$("#"+id+" img.preimg").remove();
	$("#"+id+" .up-tips").css("display","block");
	$("#"+id+" .up-input").css("display","block");
	$("#"+uploaderid).html("");
}

function edit(){
	window.location = "basicinfo";
}

function adduploadForIDN(id){
	var str = '<div class="up-tips" style="padding-top:35px;padding-left:28px;">'
		+'<span>左手持身份证正面半身照</span></div>'
		+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
    	+'<input type="file" name="idcardp_upload" id="idcardp_upload" /></div>';
	
	$("#idcardpictrue").html(str);
	bindUploadIdN();
    $("#"+id+" a").remove();
}

function adduploadForIDP(id){
	var str = '<div class="up-tips" style="padding-top:35px;padding-left:62px;">'
		+'<span>身份证反面照</span></div>'
    	+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
        +'<input type="file" name="idcardn_upload" id="idcardn_upload" /></div>';
	
	$("#idcardpictrue_n").html(str);
	bindUploadIdP();
    $("#"+id+" a").remove();
}

function adduploadForSzhy(id){
	var str = '<div class="up-tips" style="padding-top:45px;padding-left:77px;">'
		+'<span>营业执照</span></div>'
		+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
		+'<input type="file" name="szhy_upload" id="szhy_upload" /></div>';
	
	$("#szhy").html(str);
	bindUploadSzhy();
    $("#"+id+" a").remove();
}

function adduploadForYyzh(id){
	var str = '<div class="up-tips" style="padding-top:45px;padding-left:77px;">'
		+'<span>营业执照</span></div>'
    	+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
        +'<input type="file" name="yyzh_upload" id="yyzh_upload" /></div>';
	
	$("#yyzh").html(str);
	bindUploadYyzh();
    $("#"+id+" a").remove();
}

function adduploadForSwdj(id){
	var str = '<div class="up-tips" style="padding-top:45px;padding-left:70px;">'
		+'<span>税务登记证</span></div>'
    	+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
        +'<input type="file" name="swdj_upload" id="swdj_upload" /></div>';
	
	$("#swdj").html(str);
	bindUploadSwdj();
    $("#"+id+" a").remove();
}

function adduploadForZzjg(id){
	var str = '<div class="up-tips" style="padding-top:45px;padding-left:70px;">'
		+'<span>组织机构证</span></div>'
    	+'<div class="up-input" style="padding-top:8px;margin-left:43px;clear:both;">'
        +'<input type="file" name="zzjg_upload" id="zzjg_upload" /></div>';
	
	$("#zzjg").html(str);
	bindUploadZzjg();
    $("#"+id+" a").remove();
}

var wait = 3;
var zurl = "";
function timeOut(){  
    if(wait==0){  
        if(!isEmpty(zurl)){
        	window.location = zurl;
        }  
    }else{                    
        setTimeout(function(){  
            wait--;  
            timeOut();  
        },1000)  
    }  
}
function submitInfo(){
	if(checkValue()){
		var username = $("#username").val();
		var idcard = $("#idcard").val();
		var nprovince = $("#nowaddress_province").val();
		var ncity = $("#nowaddress_city").val();
		var ndistrict = $("#nowaddress_district").val();
		var ndetail = $("#nowaddress_detail").val();
		var wprovince = $("#workaddress_province").val();
		var wcity = $("#workaddress_city").val();
		var wdistrict = $("#workaddress_district").val();
		var wdetail = $("#workaddress_detail").val();
		var company = $("#company").val();
		
		var pdata = {"username":username,"idcard":idcard,"nprovince":nprovince,"ncity":ncity,
				"ndistrict":ndistrict,"ndetail":ndetail,"wprovince":wprovince,"wcity":wcity,
				"wdistrict":wdistrict,"wdetail":wdetail,"company":company,"idcardpurl":idcardpurl,
				"idcardnurl":idcardnurl,"idtype":idtype,"szhyurl":szhyurl,"yyzhurl":yyzhurl,
				"zzjgurl":zzjgurl,"swdjurl":swdjurl};

		$.ajax({
			url: "dbasicinfo",
			data: JSON.stringify(pdata),
			contentType: "application/json",
			dataType: 'json',
			type: "POST",
			success: function (result) { 
				console.log(result);
				if(result.status == 200){
					wait = 3;
					zurl = result.data;
					timeOut();
				    zeroModal.success({
				    	content: '信息提交成功了，完善店铺信息吧！',
			            contentDetail: '3s后自动跳转，点击确认可直接进入~',
			            okFn: function() {
			            	window.location = zurl;
			            }
				    });
				}else{
					zeroModal.alert(result.data);
				}
	        }
		});
	}
}

function checkValue(){
	if(!validate_username() || !validate_idcard() 
			|| !validate_naddr() || !validate_waddr() 
			|| !validate_idcardurl() || !validate_company() || !validate_yyzz()){
		return false;
	}
	return true;
}

function validate_username(){
	var value = $("#username").val();
	if(isEmpty(value) || value.length < 2){
		$("#err-username").html("真实姓名不能少于两个字");
		$("#username").focus();
		return false;
	}else{
		$("#err-username").html("");
	}
	return true;
}

function validate_idcard(){
	var value = $("#idcard").val();
	var pattern = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|[xX])$/;
	if(isEmpty(value) || !pattern.test(value)){
		$("#err-idcard").html("身份证格式不正确");
		$("#idcard").focus();
		return false;
	}else{
		$("#err-idcard").html("");
	}
	return true;
}

function validate_naddr(){
	var value = $("#nowaddress_detail").val();
	if(isEmpty(value)){
		$("#err-naddr").html("地址不能为空");
		$("#nowaddress_detail").focus();
		return false;
	}else{
		$("#err-naddr").html("");
	}
	return true;
}

function validate_waddr(){
	var value = $("#workaddress_detail").val();
	if(isEmpty(value)){
		$("#err-waddr").html("地址不能为空");
		$("#workaddress_detail").focus();
		return false;
	}else{
		$("#err-waddr").html("");
	}
	return true;
}

function validate_idcardurl(){
	if(isEmpty(idcardpurl) || isEmpty(idcardnurl)){
		$("#err-idcardurl").html("身份证照不能为空");
		$("#idcardpictrue").focus();
		return false;
	}else{
		$("#err-idcardurl").html("");
	}
	return true;
}

function validate_company(){
	var value = $("#company").val();
	if(isEmpty(value) || value.length < 2){
		$("#err-company").html("企业名称不正确");
		return false;
	}else{
		$("#err-company").html("");
	}
	return true;
}

function validate_yyzz(){
	if(idtype == 0){
		if(isEmpty(szhyurl)){
			$("#err-yyzz").html("营业执照不能为空");
			return false;
		}else{
			$("#err-yyzz").html("");
		}
		return true;
	}else if(idtype == 1){
		if(isEmpty(yyzhurl) || isEmpty(swdjurl) || isEmpty(zzjgurl)){
			$("#err-yyzz").html("营业执照不能为空");
			return false;
		}else{
			$("#err-yyzz").html("");
		}
		return true;
	}
}

function changeIdtype(type){
	if(type == 0){
		idtype = 0;
		$("#a-szhy").removeClass("input-radio");
		$("#a-szhy").addClass("input-radio-s");
		$("#a-nszhy").removeClass("input-radio-s");
		$("#a-nszhy").addClass("input-radio");
		$("#szhy-div").css("display","block");
		$("#nszhy-div").css("display","none");
		$("#nszhy-div-sample").css("display","none");
	}else if(type == 1){
		idtype = 1;
		$("#a-szhy").addClass("input-radio");
		$("#a-szhy").removeClass("input-radio-s");
		$("#a-nszhy").addClass("input-radio-s");
		$("#a-nszhy").removeClass("input-radio");
		$("#szhy-div").css("display","none");
		$("#nszhy-div").css("display","block");
		$("#nszhy-div-sample").css("display","block");
	}
}

function bindUploadIdN(){
	//身份证正面
    $("#idcardp_upload")  
            .uploadify(  
                    {  
                        'auto' : true,  
                        'method' : "get",  
                        'formData' : {  
                            'folder' : 'file'  
                        },  
                        'height' : 30,  
                        'swf' : '../uploadify/uploadify.swf', // flash  
                        'uploader' : '../upload/up', //  
                        'width' : 120,  
                        'fileTypeDesc' : '支持多种文件格式',  
                        'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG;',  
                        'fileSizeLimit' : '2MB',  
                        'buttonText' : '点击上传',  
                        'uploadLimit' : 1,  
                        'successTimeout' : 5,  
                        'requeueErrors' : false,  
                        'removeTimeout' : 10,  
                        'removeCompleted' : false,  
                        'queueSizeLimit' : 10,  
                        'queueID' : 'idcard_uploader',  
                        'progressData' : 'percentage',  
                        'onInit' : function() {  
                        },  
                        'onUploadSuccess' : function(file, data, response) {  
                        	$("#idcardpictrue .up-tips").css("display","none");
                        	$("#idcardpictrue .up-input").css("display","none");
                            $("#idcardpictrue").prepend(  
                                    '<img class="preimg" height="130" alt="" src="../upload/'  
                                            + encodeURI(data)  
                                            + '"/>');  
                            idcardpurl = data;
                            $("#idcard_uploader .cancel a").remove();
                            $("#idcard_uploader .cancel").prepend("<a href=\"javascript:reSelect('idcardpictrue','idcard_uploader')\">X</a>");
                        },
                        'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                        	console.log('上传错误');
                        }
                    });  
}

function bindUploadIdP(){
	//身份证反面照
    $("#idcardn_upload").uploadify(  
            {  
                'auto' : true,  
                'method' : "get",  
                'formData' : {  
                    'folder' : 'file'  
                },  
                'height' : 30,  
                'swf' : '../uploadify/uploadify.swf', // flash  
                'cancelImg': '../uploadify/uploadify-cancel.png', 
                'uploader' : '../upload/up', //  
                'width' : 120,  
                'fileTypeDesc' : '支持多种文件格式',  
                'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG',  
                'fileSizeLimit' : '2MB',  
                'buttonText' : '点击上传',  
                'uploadLimit' : 1,  
                'successTimeout' : 5,  
                'requeueErrors' : false,  
                'removeTimeout' : 10,  
                'removeCompleted' : false,  
                'queueSizeLimit' : 10,  
                'queueID' : 'idcardn_uploader',  
                'progressData' : 'percentage',  
                'onInit' : function() {  
                },  
                'onUploadSuccess' : function(file, data, response) {  
                	$("#idcardpictrue_n .up-tips").css("display","none");
                	$("#idcardpictrue_n .up-input").css("display","none");
                    $("#idcardpictrue_n").prepend(  
                            '<img class="preimg" height="130" alt="" src="../upload/'  
                                    + encodeURI(data)  
                                    + '"/>');  
                    idcardnurl = data;
                    $("#idcardn_uploader .cancel a").remove();
                    $("#idcardn_uploader .cancel").prepend("<a href=\"javascript:reSelect('idcardpictrue_n','idcardn_uploader')\">X</a>");
                    
                },
                'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                	
                }
            });
}

function bindUploadSzhy(){
	//szhy
    $("#szhy_upload").uploadify(  
            {  
                'auto' : true,  
                'method' : "get",  
                'formData' : {  
                    'folder' : 'file'  
                },  
                'height' : 30,  
                'swf' : '../uploadify/uploadify.swf', // flash  
                'cancelImg': '../uploadify/uploadify-cancel.png', 
                'uploader' : '../upload/up', //  
                'width' : 120,  
                'fileTypeDesc' : '支持多种文件格式',  
                'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG',  
                'fileSizeLimit' : '2MB',  
                'buttonText' : '点击上传',  
                'uploadLimit' : 1,  
                'successTimeout' : 5,  
                'requeueErrors' : false,  
                'removeTimeout' : 10,  
                'removeCompleted' : false,  
                'queueSizeLimit' : 10,  
                'queueID' : 'szhy_uploader',  
                'progressData' : 'percentage',  
                'onInit' : function() {  
                },  
                'onUploadSuccess' : function(file, data, response) {  
                	$("#szhy .up-tips").css("display","none");
                	$("#szhy .up-input").css("display","none");
                    $("#szhy").prepend(  
                            '<img class="preimg" height="149" alt="" src="../upload/'  
                                    + encodeURI(data)  
                                    + '"/>');  
                    szhyurl = data;
                    $("#szhy_uploader .cancel a").remove();
                    $("#szhy_uploader .cancel").prepend("<a href=\"javascript:reSelect('szhy','szhy_uploader')\">X</a>");
                },
                'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                	
                }
            });
}

function bindUploadYyzh(){
	//yyzh
    $("#yyzh_upload").uploadify(  
            {  
                'auto' : true,  
                'method' : "get",  
                'formData' : {  
                    'folder' : 'file'  
                },  
                'height' : 30,  
                'swf' : '../uploadify/uploadify.swf', // flash  
                'cancelImg': '../uploadify/uploadify-cancel.png', 
                'uploader' : '../upload/up', //  
                'width' : 120,  
                'fileTypeDesc' : '支持多种文件格式',  
                'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG',  
                'fileSizeLimit' : '2MB',  
                'buttonText' : '点击上传',  
                'uploadLimit' : 1,  
                'successTimeout' : 5,  
                'requeueErrors' : false,  
                'removeTimeout' : 10,  
                'removeCompleted' : false,  
                'queueSizeLimit' : 10,  
                'queueID' : 'yyzh_uploader',  
                'progressData' : 'percentage',  
                'onInit' : function() {  
                },  
                'onUploadSuccess' : function(file, data, response) { 
                	$("#yyzh .up-tips").css("display","none");
                	$("#yyzh .up-input").css("display","none");
                    $("#yyzh").prepend(  
                            '<img class="preimg" height="149" alt="" src="../upload/'  
                                    + encodeURI(data)  
                                    + '"/>');  
                    yyzhurl = data;
                    $("#yyzh_uploader .cancel a").remove();
                    $("#yyzh_uploader .cancel").prepend("<a href=\"javascript:reSelect('yyzh','yyzh_uploader')\">X</a>");

                },
                'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                	
                }
            });
}

function bindUploadZzjg(){
	//zzjg
    $("#zzjg_upload").uploadify(  
            {  
                'auto' : true,  
                'method' : "get",  
                'formData' : {  
                    'folder' : 'file'  
                },  
                'height' : 30,  
                'swf' : '../uploadify/uploadify.swf', // flash  
                'cancelImg': '../uploadify/uploadify-cancel.png', 
                'uploader' : '../upload/up', //  
                'width' : 120,  
                'fileTypeDesc' : '支持多种文件格式',  
                'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG',  
                'fileSizeLimit' : '2MB',  
                'buttonText' : '点击上传',  
                'uploadLimit' : 100,  
                'successTimeout' : 5,  
                'requeueErrors' : false,  
                'removeTimeout' : 10,  
                'removeCompleted' : false,  
                'queueSizeLimit' : 10,  
                'queueID' : 'zzjg_uploader',  
                'progressData' : 'percentage',  
                'onInit' : function() {  
                },  
                'onUploadSuccess' : function(file, data, response) {
                	$("#zzjg .up-tips").css("display","none");
                	$("#zzjg .up-input").css("display","none");
                    $("#zzjg").prepend(  
                            '<img class="preimg" height="149" alt="" src="../upload/'  
                                    + encodeURI(data)  
                                    + '"/>');  
                    zzjgurl = data;
                    $("#zzjg_uploader .cancel a").remove();
                    $("#zzjg_uploader .cancel").prepend("<a href=\"javascript:reSelect('zzjg','zzjg_uploader')\">X</a>");

                },
                'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                	
                }
            });
}

function bindUploadSwdj(){
	//swdj
    $("#swdj_upload").uploadify(  
            {  
                'auto' : true,  
                'method' : "get",  
                'formData' : {  
                    'folder' : 'file'  
                },  
                'height' : 30,  
                'swf' : '../uploadify/uploadify.swf', // flash  
                'cancelImg': '../uploadify/uploadify-cancel.png', 
                'uploader' : '../upload/up', //  
                'width' : 120,  
                'fileTypeDesc' : '支持多种文件格式',  
                'fileTypeExts' : '.JPG;.bmp;.png;.BMP;.jpg;.PNG',  
                'fileSizeLimit' : '2MB',  
                'buttonText' : '点击上传',  
                'uploadLimit' : 1,  
                'successTimeout' : 5,  
                'requeueErrors' : false,  
                'removeTimeout' : 10,  
                'removeCompleted' : false,  
                'queueSizeLimit' : 10,  
                'queueID' : 'swdj_uploader',  
                'progressData' : 'percentage',  
                'onInit' : function() {  
                },  
                'onUploadSuccess' : function(file, data, response) {  
                	$("#swdj .up-tips").css("display","none");
                	$("#swdj .up-input").css("display","none");
                    $("#swdj").prepend(  
                            '<img class="preimg" height="149" alt="" src="../upload/'  
                                    + encodeURI(data)  
                                    + '"/>');  
                    swdjurl = data;
                    $("#swdj_uploader .cancel a").remove();
                    $("#swdj_uploader .cancel").prepend("<a href=\"javascript:reSelect('swdj','swdj_uploader')\">X</a>");
                },
                'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {
                	
                }
            });
}
    $(function() {  
    	bindUploadIdN();
    	bindUploadIdP();
    	bindUploadSzhy();
    	bindUploadYyzh();
    	bindUploadZzjg();
    	bindUploadSwdj();
    });  